package com.pan.admin.module.loanUser.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.github.pagehelper.Page;
import com.pan.admin.common.annotation.DataScope;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.loanUser.dao.LoanUserFormFieldDao;
import com.pan.admin.module.loanUser.entity.LoanUserFormField;
import com.pan.admin.module.loanUser.entity.po.LoanUserFormFieldPO;
import com.pan.admin.module.loanUser.entity.vo.LoanUserFormFieldVo;
import com.pan.admin.module.loanUser.service.LoanUserFormFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户资质列表Service业务层处理
 *
 * @author ts
 * @date 2023-10-11
 */
@Service
public class LoanUserFormFieldServiceImpl implements LoanUserFormFieldService {
    @Autowired
    private LoanUserFormFieldDao loanUserFormFieldDao;

    /**
     * 根据ID获取列表用户资质列表
     *
     * @param id 用户资质列表主键
     * @return 用户资质列表
     */
    @Override
    public LoanUserFormField getById(Long id) {
        return loanUserFormFieldDao.getById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<LoanUserFormFieldPO> getUserFormField(Integer loanUserId) {
        return loanUserFormFieldDao.getUserFormField(loanUserId);
    }

    /**
     * 查询用户资质列表列表
     *
     * @param loanUserFormField 用户资质列表
     * @return 用户资质列表
     */
    @Override
    public List<LoanUserFormField> list(LoanUserFormField loanUserFormField) {
        return loanUserFormFieldDao.list(loanUserFormField);
    }

    /**
     * 新增用户资质列表
     *
     * @param loanUserFormField 用户资质列表
     * @return 结果
     */
    @Override
    public int insertLoanUserFormField(LoanUserFormField loanUserFormField) {
        loanUserFormField.setCreateTime(DateUtils.getNowDate());
        return loanUserFormFieldDao.insertLoanUserFormField(loanUserFormField);
    }

    /**
     * 修改用户资质列表
     *
     * @param loanUserFormField 用户资质列表
     * @return 结果
     */
    @Override
    public int updateLoanUserFormField(LoanUserFormField loanUserFormField) {
        return loanUserFormFieldDao.updateLoanUserFormField(loanUserFormField);
    }


    /**
     * 查询用户资质列表列表 数据
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LoanUserFormFieldVo> userFormFieldList(LoanUserFormFieldVo loanUserFormField) {
        // 初始化结果数据
        Integer productId = SecurityUtils.getLoginUser().getUser().getProductId();
        loanUserFormField.setLoanProductId(productId);
        // 初始化processedData并填充默认值
        List<LoanUserFormFieldVo> fieldVoList = loanUserFormFieldDao.userFormFieldList(loanUserFormField);
        // 对查询结果进行分页和排序
        Page<LoanUserFormFieldVo> page = (Page<LoanUserFormFieldVo>) fieldVoList;
        // 获取分页结果
        List<LoanUserFormFieldVo> pageResult = page.getResult();


        if (!ValidParam.isEmpty(fieldVoList)) {

            for (LoanUserFormFieldVo userFormFieldVo : fieldVoList) {
                List<LoanUserFormFieldVo> list1 = loanUserFormFieldDao.selectByPhone(userFormFieldVo.getPhone());
                List<Map<String, String>> mapList = new ArrayList<>();

                if (!ValidParam.isEmpty(fieldVoList)) {
                    for (LoanUserFormFieldVo data : list1) {
                        Map<String, String> map = new HashMap<>();
                        String phone = userFormFieldVo.getPhone();
                        String fieldId = data.getFieldId().toString();
                        String fieldValue = data.getFieldValue();
                        String channelName = userFormFieldVo.getChannelName();
                        map.put(fieldId, fieldValue);
                        map.put("channelName", channelName);
                        map.put("phone", phone);
                        mapList.add(map);
                    }
                }
                userFormFieldVo.setMap(mapList);
            }
        }

        return fieldVoList;
    }

}
